home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / f_fill.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  8.6 KB  |  286 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <windows.h>
  19. #include <commctrl.h>
  20. #include <crtdbg.h>
  21. #include <stdio.h>
  22.  
  23. #include "VideoSource.h"
  24. #include "ClippingControl.h"
  25.  
  26. #include "resource.h"
  27. #include "filter.h"
  28. #include "gui.h"
  29.  
  30. #include "ScriptInterpreter.h"
  31. #include "ScriptValue.h"
  32. #include "ScriptError.h"
  33.  
  34. extern HINSTANCE g_hInst;
  35. extern VideoSource *inputVideoAVI;
  36.  
  37. ///////////////////////
  38.  
  39. typedef struct MyFilterData {
  40.     long x1, y1, x2, y2;
  41.     COLORREF color, color_temp;
  42.     HBRUSH hbrColor;
  43. } MyFilterData;
  44.  
  45. static int fill_run(const FilterActivation *fa, const FilterFunctions *ff) {
  46.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  47.  
  48.     unsigned long w,h;
  49.     Pixel *dst, *dst2;
  50.     Pixel c = (Pixel)(((mfd->color & 0xff)<<16) | (mfd->color & 0xff00) | ((mfd->color & 0xff0000)>>16));
  51.  
  52.     if (mfd->x1+mfd->x2 >= fa->dst.w) return 0;
  53.     if (mfd->y1+mfd->y2 >= fa->dst.h) return 0;
  54.  
  55.     dst = (Pixel *)((char *)((Pixel *)fa->dst.data + mfd->x1) + mfd->y2*fa->dst.pitch);
  56.  
  57.     h = fa->dst.h - mfd->y1 - mfd->y2;
  58.     do {
  59.         dst2 = dst;
  60.  
  61.         w = fa->dst.w - mfd->x1 - mfd->x2;
  62.         do {
  63.             *dst2++ = c;
  64.         } while(--w);
  65.  
  66.         dst = (unsigned long *)((char *)dst + fa->dst.pitch);
  67.     } while(--h);
  68.  
  69.     return 0;
  70. }
  71.  
  72. static long fill_param(FilterActivation *fa, const FilterFunctions *ff) {
  73.     fa->dst.offset = fa->src.offset;
  74.     return 0;
  75. }
  76.  
  77. static BOOL APIENTRY fillDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) {
  78.     MyFilterData *mfd;
  79.  
  80.     switch (message)
  81.     {
  82.         case WM_INITDIALOG:
  83.             {
  84.                 ClippingControlBounds ccb;
  85.                 LONG hborder, hspace;
  86.                 RECT rw, rc, rcok, rccancel, rcpickcolor, rccolor;
  87.                 HWND hWnd, hWndCancel, hWndPickColor, hWndColor;
  88.                 long x,y;
  89.  
  90.                 mfd = (MyFilterData *)lParam;
  91.                 SetWindowLong(hDlg, DWL_USER, (LONG)mfd);
  92.  
  93.                 hWnd = GetDlgItem(hDlg, IDC_BORDERS);
  94.                 ccb.x1    = mfd->x1;
  95.                 ccb.x2    = mfd->x2;
  96.                 ccb.y1    = mfd->y1;
  97.                 ccb.y2    = mfd->y2;
  98.  
  99.                 mfd->color_temp = mfd->color;
  100.                 mfd->hbrColor = CreateSolidBrush(mfd->color);
  101.  
  102.                 if (inputVideoAVI) {
  103.                     BITMAPINFOHEADER *bmi = inputVideoAVI->getImageFormat();
  104.                     SendMessage(hWnd, CCM_SETBITMAPSIZE, 0, MAKELONG(bmi->biWidth,bmi->biHeight));
  105.                 } else
  106.                     SendMessage(hWnd, CCM_SETBITMAPSIZE, 0, MAKELONG(320,240));
  107.  
  108.                 SendMessage(hWnd, CCM_SETCLIPBOUNDS, 0, (LPARAM)&ccb);
  109.  
  110.                 guiPositionInitFromStream(hWnd);
  111.  
  112.                 GetWindowRect(hDlg, &rw);
  113.                 GetWindowRect(hWnd, &rc);
  114.                 hborder = rc.left - rw.left;
  115.                 ScreenToClient(hDlg, (LPPOINT)&rc.left);
  116.                 ScreenToClient(hDlg, (LPPOINT)&rc.right);
  117.  
  118.                 SetWindowPos(hDlg, NULL, 0, 0, (rc.right - rc.left) + hborder*2, (rw.bottom-rw.top)+(rc.bottom-rc.top), SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE);
  119.  
  120.                 hWndCancel = GetDlgItem(hDlg, IDCANCEL);
  121.                 hWnd = GetDlgItem(hDlg, IDOK);
  122.                 hWndPickColor = GetDlgItem(hDlg, IDC_PICK_COLOR);
  123.                 hWndColor = GetDlgItem(hDlg, IDC_COLOR);
  124.                 GetWindowRect(hWnd, &rcok);
  125.                 GetWindowRect(hWndCancel, &rccancel);
  126.                 GetWindowRect(hWndPickColor, &rcpickcolor);
  127.                 GetWindowRect(hWndColor, &rccolor);
  128.                 hspace = rccancel.left - rcok.right;
  129.                 ScreenToClient(hDlg, (LPPOINT)&rcok.left);
  130.                 ScreenToClient(hDlg, (LPPOINT)&rcok.right);
  131.                 ScreenToClient(hDlg, (LPPOINT)&rccancel.left);
  132.                 ScreenToClient(hDlg, (LPPOINT)&rccancel.right);
  133.                 ScreenToClient(hDlg, (LPPOINT)&rcpickcolor.left);
  134.                 ScreenToClient(hDlg, (LPPOINT)&rcpickcolor.right);
  135.                 ScreenToClient(hDlg, (LPPOINT)&rccolor.left);
  136.                 ScreenToClient(hDlg, (LPPOINT)&rccolor.right);
  137.  
  138.                 x = rc.right;
  139.                 y = rc.bottom - rc.top;
  140.  
  141.                 x -= (rccancel.right - rccancel.left);
  142.                 SetWindowPos(hWndCancel    , NULL, x           ,    rccancel.top + y, 0,0,SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE);
  143.  
  144.                 x -= (rcok.right - rcok.left);
  145.                 SetWindowPos(hWnd        , NULL, x - hspace  ,        rcok.top + y, 0,0,SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE);
  146.  
  147.                 SetWindowPos(hWndPickColor, NULL, rcpickcolor.left, rcpickcolor.top + y, 0,0,SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE);
  148.  
  149.                 SetWindowPos(hWndColor    , NULL, rccolor.left,        rccolor.top + y, 0,0,SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE);
  150.             }
  151.  
  152.             return (TRUE);
  153.  
  154.         case WM_COMMAND:
  155.             switch(LOWORD(wParam)) {
  156.             case IDOK:
  157.                 {
  158.                     ClippingControlBounds ccb;
  159.  
  160.                     mfd = (MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  161.                     SendMessage(GetDlgItem(hDlg, IDC_BORDERS), CCM_GETCLIPBOUNDS, 0, (LPARAM)&ccb);
  162.                     mfd->x1 = ccb.x1;
  163.                     mfd->y1 = ccb.y1;
  164.                     mfd->x2 = ccb.x2;
  165.                     mfd->y2 = ccb.y2;
  166.                     mfd->color = mfd->color_temp;
  167.                     if (mfd->hbrColor) {
  168.                         DeleteObject(mfd->hbrColor);
  169.                         mfd->hbrColor = NULL;
  170.                     }
  171.                     EndDialog(hDlg, 0);
  172.                 }
  173.                 return TRUE;
  174.             case IDCANCEL:
  175.                 mfd = (MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  176.                 if (mfd->hbrColor) {
  177.                     DeleteObject(mfd->hbrColor);
  178.                     mfd->hbrColor = NULL;
  179.                 }
  180.                 EndDialog(hDlg, 1);
  181.                 return TRUE;
  182.             case IDC_BORDERS:
  183.                 guiPositionBlit((HWND)lParam, guiPositionHandleCommand(wParam, lParam));
  184.                 return TRUE;
  185.  
  186.             case IDC_PICK_COLOR:
  187.                 mfd = (MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  188. /*
  189.                 {
  190.                     CHOOSECOLOR cc;                 // common dialog box structure 
  191.                     static COLORREF acrCustClr[16]; // array of custom colors 
  192.  
  193.                     // Initialize CHOOSECOLOR
  194.                     memset(&cc, 0, sizeof(CHOOSECOLOR));
  195.                     cc.lStructSize    = sizeof(CHOOSECOLOR);
  196.                     cc.hwndOwner    = hDlg;
  197.                     cc.lpCustColors    = (LPDWORD) acrCustClr;
  198.                     cc.rgbResult    = mfd->color_temp;
  199.                     cc.Flags        = CC_FULLOPEN | CC_RGBINIT;
  200.  
  201.                     if (ChooseColor(&cc)==TRUE) {
  202.                         DeleteObject(mfd->hbrColor);
  203.                         mfd->hbrColor = CreateSolidBrush(mfd->color_temp = cc.rgbResult);
  204.                         RedrawWindow(GetDlgItem(hDlg, IDC_COLOR), NULL, NULL, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  205.                     }
  206.                 }
  207. */
  208.                 if (guiChooseColor(hDlg, mfd->color_temp)) {
  209.                     DeleteObject(mfd->hbrColor);
  210.                     mfd->hbrColor = CreateSolidBrush(mfd->color_temp);
  211.                     RedrawWindow(GetDlgItem(hDlg, IDC_COLOR), NULL, NULL, RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  212.                 }
  213.  
  214.                 return TRUE;
  215.             }
  216.             break;
  217.  
  218.         case WM_NOTIFY:
  219.             guiPositionBlit(((NMHDR *)lParam)->hwndFrom, guiPositionHandleNotify(wParam, lParam));
  220.             break;
  221.  
  222.         case WM_CTLCOLORSTATIC:
  223.             mfd = (MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  224.             return (BOOL)mfd->hbrColor;
  225.             break;
  226.     }
  227.     return FALSE;
  228. }
  229.  
  230. static int fill_config(FilterActivation *fa, const FilterFunctions *ff, HWND hWnd) {
  231.     return DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_FILTER_FILL), hWnd, fillDlgProc, (LONG)fa->filter_data);
  232. }
  233.  
  234. static void fill_string(const FilterActivation *fa, const FilterFunctions *ff, char *buf) {
  235.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  236.  
  237.     wsprintf(buf, " (color: #%02X%02X%02X)", mfd->color&0xff, (mfd->color>>8)&0xff, (mfd->color>>16)&0xff);
  238. }
  239.  
  240. static void fill_script_config(IScriptInterpreter *isi, void *lpVoid, CScriptValue *argv, int argc) {
  241.     FilterActivation *fa = (FilterActivation *)lpVoid;
  242.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  243.  
  244.     mfd->x1        = argv[0].asInt();
  245.     mfd->y1        = argv[1].asInt();
  246.     mfd->x2        = argv[2].asInt();
  247.     mfd->y2        = argv[3].asInt();
  248.     mfd->color    = argv[4].asInt();
  249. }
  250.  
  251. static ScriptFunctionDef fill_func_defs[]={
  252.     { (ScriptFunctionPtr)fill_script_config, "Config", "0iiiii" },
  253.     { NULL },
  254. };
  255.  
  256. static CScriptObject fill_obj={
  257.     NULL, fill_func_defs
  258. };
  259.  
  260. static bool fill_script_line(FilterActivation *fa, const FilterFunctions *ff, char *buf, int buflen) {
  261.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  262.  
  263.     _snprintf(buf, buflen, "Config(%d,%d,%d,%d,0x%06lx)", mfd->x1, mfd->y1, mfd->x2, mfd->y2, mfd->color);
  264.  
  265.     return true;
  266. }
  267.  
  268. FilterDefinition filterDef_fill={
  269.     0,0,NULL,
  270.     "fill",
  271.     "Fills an image rectangle with a color.",
  272.     NULL,NULL,
  273.     sizeof(MyFilterData),
  274.     NULL,NULL,
  275.     fill_run,
  276.     fill_param,
  277.     fill_config,
  278.     fill_string,
  279.     NULL,
  280.     NULL,
  281.  
  282.     &fill_obj,
  283.     fill_script_line,
  284. };
  285.  
  286.